﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>




int* plusOne(int* digits, int digitsSize, int* returnSize) {
    int i = digitsSize-1;
    if (digits[i] <= 8)//最后一位是小于等于8
    {
        digits[i] += 1;
        *returnSize = digitsSize;
        return digits;
    }
    else//最后一位等于9
    {
        digits[i] = 0;
        i--;

        while (i >= 0) {
            if (digits[i] <= 8)
            {
                digits[i] += 1;
                *returnSize = digitsSize;
                return digits;
            }
            else
            {
                digits[i] = 0;
                i--;
            }
        }
    }
    //9 9 9 ->1 0 0 0全是9的
    int* newdigits = (int*)malloc((digitsSize + 1) * sizeof(int));
    newdigits[0] = 1;
    int j = 0;
    for (j = 1; j < digitsSize + 1; j++)
    {
        newdigits[j] = 0;
    }

    *returnSize = digitsSize + 1;
    return newdigits;
}


/**
 * 打印数组
 */
void printArray(int* arr, int size) {
    printf("[");
    for (int i = 0; i < size; i++) {
        printf("%d", arr[i]);
        if (i < size - 1) {
            printf(", ");
        }
    }
    printf("]");
}

/**
 * 运行测试用例
 */
void runTest() {
    printf("=== 加一算法测试用例 ===\n\n");

    // 测试用例1: 正常情况，无进位
    {
        int digits[] = { 1, 2, 3 };
        int expected[] = { 1, 2, 4 };
        int returnSize;
        int* result = plusOne(digits, 3, &returnSize);

        printf("测试用例1 - ");
        printf("输入: "); printArray(digits, 3);
        printf(" 输出: "); printArray(result, returnSize);
        printf(" 期望: "); printArray(expected, 3);

        int passed = (returnSize == 3);
        for (int i = 0; i < returnSize && passed; i++) {
            if (result[i] != expected[i]) passed = 0;
        }
        printf(" %s\n", passed ? "通过" : "失败");

        if (result != digits) free(result);
    }

    // 测试用例2: 有进位但不需要增加位数
    {
        int digits[] = { 4, 3, 2, 1 };
        int expected[] = { 4, 3, 2, 2 };
        int returnSize;
        int* result = plusOne(digits, 4, &returnSize);

        printf("测试用例2 - ");
        printf("输入: "); printArray(digits, 4);
        printf(" 输出: "); printArray(result, returnSize);
        printf(" 期望: "); printArray(expected, 4);

        int passed = (returnSize == 4);
        for (int i = 0; i < returnSize && passed; i++) {
            if (result[i] != expected[i]) passed = 0;
        }
        printf(" %s\n", passed ? "通过" : "失败");

        if (result != digits) free(result);
    }

    // 测试用例3: 需要增加位数的情况（全为9）
    {
        int digits[] = { 9, 9, 9 };
        int expected[] = { 1, 0, 0, 0 };
        int returnSize;
        int* result = plusOne(digits, 3, &returnSize);

        printf("测试用例3 - ");
        printf("输入: "); printArray(digits, 3);
        printf(" 输出: "); printArray(result, returnSize);
        printf(" 期望: "); printArray(expected, 4);

        int passed = (returnSize == 4);
        for (int i = 0; i < returnSize && passed; i++) {
            if (result[i] != expected[i]) passed = 0;
        }
        printf(" %s\n", passed ? "通过" : "失败");

        if (result != digits) free(result);
    }

    // 测试用例4: 单个数字
    {
        int digits[] = { 9 };
        int expected[] = { 1, 0 };
        int returnSize;
        int* result = plusOne(digits, 1, &returnSize);

        printf("测试用例4 - ");
        printf("输入: "); printArray(digits, 1);
        printf(" 输出: "); printArray(result, returnSize);
        printf(" 期望: "); printArray(expected, 2);

        int passed = (returnSize == 2);
        for (int i = 0; i < returnSize && passed; i++) {
            if (result[i] != expected[i]) passed = 0;
        }
        printf(" %s\n", passed ? "通过" : "失败");

        if (result != digits) free(result);
    }

    // 测试用例5: 中间有进位的情况
    {
        int digits[] = { 1, 9, 9 };
        int expected[] = { 2, 0, 0 };
        int returnSize;
        int* result = plusOne(digits, 3, &returnSize);

        printf("测试用例5 - ");
        printf("输入: "); printArray(digits, 3);
        printf(" 输出: "); printArray(result, returnSize);
        printf(" 期望: "); printArray(expected, 3);

        int passed = (returnSize == 3);
        for (int i = 0; i < returnSize && passed; i++) {
            if (result[i] != expected[i]) passed = 0;
        }
        printf(" %s\n", passed ? "通过" : "失败");

        if (result != digits) free(result);
    }

    printf("\n=== 测试完成 ===\n");
}

int main() {
    runTest();
    return 0;
}


